package com.ms.education.ucenter.controller;

import cn.dev33.satoken.annotation.SaCheckLogin;
import cn.dev33.satoken.annotation.SaCheckPermission;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ms.education.R;
import com.ms.education.ucenter.entity.TeacherApplication;
import com.ms.education.ucenter.service.TeacherApplicationService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author ms
 * @since 2023-02-28
 */
@RestController
@RequestMapping("/ucenter/teacher-application")
public class TeacherApplicationController {

    @Autowired
    private TeacherApplicationService teacherApplicationService;


    @ApiOperation("查询用户的申请")
    @GetMapping("getUserApply/{userId}")
    public R getUserApply(@PathVariable("userId") Integer userId) {
        TeacherApplication teacherApplication = teacherApplicationService.getUserApply(userId);
        return R.ok().data("apply", teacherApplication);
    }

    @SaCheckLogin
    @ApiOperation("用户提交申请")
    @PostMapping("applyForTeacher")
    public R applyForTeacher(@RequestBody TeacherApplication teacherApplication) {
        teacherApplicationService.applyForTeacher(teacherApplication);
        return R.ok();
    }

    @SaCheckPermission("teacherApply.list")
    @ApiOperation("查询用户申请")
    @PostMapping("pageUserApply/{current}/{limit}")
    public R pageUserApply(@PathVariable("current") long current,
                           @PathVariable("limit") long limit,
                           @RequestBody TeacherApplication teacherApplication) {
        Page<TeacherApplication> page = teacherApplicationService.pageUserApply(current, limit, teacherApplication);
        return R.ok().data("rows", page.getTotal()).data("applies", page.getRecords());
    }

    @SaCheckPermission("teacherApply.doWith")
    @ApiOperation("申请通过")
    @PostMapping("successApply/{id}")
    public R successApply(@PathVariable("id") Integer id) {
        teacherApplicationService.successApply(id);
        return R.ok();
    }

    @ApiOperation("申请不通过")
    @PostMapping("failApply/{id}")
    public R failApply(@PathVariable("id") Integer id) {
        teacherApplicationService.failApply(id);
        return R.ok();
    }

    @ApiOperation("根据id查询")
    @GetMapping("getById/{id}")
    public R getById(@PathVariable("id") Integer id) {
        TeacherApplication apply = teacherApplicationService.getById(id);
        return R.ok().data("apply", apply);
    }
}
